<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\balancediscount\model;

class OrderList extends \app\BaseModel
{
    protected $name = "massage_balance_discount_order_list";
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function orderResult($order_code, $transaction_id)
    {
        $order = $this->dataInfo(["order_code" => $order_code, "transaction_id" => ""]);
        if(!empty($order) && !empty($transaction_id)) {
            \think\facade\Db::startTrans();
            $update = ["transaction_id" => $transaction_id, "pay_type" => 2, "pay_time" => time(), "over_time" => strtotime("+" . $order["month"] . " months")];
            $res = $this->dataUpdate(["id" => $order["id"], "transaction_id" => ""], $update);
            if($res == 0) {
                \think\facade\Db::rollback();
                return false;
            }
            $user_card_model = new UserCard();
            $res = $user_card_model->dataAdd($order);
            if($res == 0) {
                \think\facade\Db::rollback();
                return false;
            }
            $integral_model = new \app\massage\model\Integral();
            $coach_model = new \app\massage\model\Coach();
            $integral_record = $integral_model->dataInfo(["order_id" => $order["id"], "type" => 2, "status" => -1]);
            if(!empty($integral_record)) {
                $integral_model->dataUpdate(["id" => $integral_record["id"]], ["status" => 1]);
                $integral = $integral_record["integral"];
                $coach_model->where(["id" => $order["coach_id"]])->update(["integral" => \think\facade\Db::Raw("integral+" . $integral)]);
            }
            $comm_model = new \app\massage\model\Commission();
            $comm_model->dataUpdate(["order_id" => $order["id"], "type" => 25, "status" => -1], ["status" => 1]);
            \think\facade\Db::commit();
            $this->commSuccess($order["id"]);
        }
        return true;
    }
    public function commSuccess($order_id)
    {
        $comm_model = new \app\massage\model\Commission();
        $integral_model = new \app\massage\model\Integral();
        $data = $comm_model->dataInfo(["order_id" => $order_id, "status" => 1, "type" => 25]);
        if(!empty($data)) {
            \think\facade\Db::startTrans();
            $integral_model->dataUpdate(["order_id" => $order_id, "type" => 3, "status" => -1], ["status" => 1]);
            $res = $comm_model->dataUpdate(["id" => $data["id"], "status" => 1], ["status" => 2, "cash_time" => time()]);
            if($res == 0) {
                \think\facade\Db::rollback();
                cacheLpush("balance_discount_comm", $order_id, $data["uniacid"]);
            }
            $water_model = new \app\massage\model\CoachWater();
            $res = $water_model->updateCash($data["uniacid"], $data["top_id"], $data["cash"], 1);
            if($res == 0) {
                \think\facade\Db::rollback();
                cacheLpush("balance_discount_comm", $order_id, $data["uniacid"]);
            }
            \think\facade\Db::commit();
        }
        return true;
    }
    public function listCommSuccess($uniacid, $i = 10)
    {
        $num = 0;
        while ($num < $i) {
            $num++;
            $data_id = cacheLpop("balance_discount_comm", $uniacid);
            if(!empty($data_id)) {
                $this->commSuccess($data_id);
            } else {
                return true;
            }
        }
        return true;
    }
}

?>